home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Icon 8.1 / mep1 / Samples / Programs / characters.icn < prev    next >
Encoding:
Text File  |  1989-05-09  |  1.7 KB  |  62 lines  |  [TEXT/PICN]

  1. ############################################################################
  2. #
  3. #  characters.icn
  4. #  
  5. #     This program tabulates characters and lists each character and
  6. #  the number of times it occurs. Characters are written using
  7. #  Icon's escape conventions.  Line termination characters and other
  8. #  control characters are included in the tabulation. The user is prompted
  9. #  with as Get File dialog box. Files are processed until the user selects
  10. #  Cancel.
  11. #  
  12. #  Options: The following options are available via the parameter string:
  13. #  
  14. #       -a   Write the summary in alphabetical order of the charac-
  15. #            ters. This is the default.
  16. #  
  17. #       -n   Write the summary in numerical order of the counts.
  18. #  
  19. #       -u   Write only the characters that occur just once.
  20. #  
  21. ############################################################################
  22. #
  23. #  Links: getopt
  24. #
  25. ############################################################################
  26.  
  27. link getopt
  28.  
  29. procedure main(args)
  30.     local ccount, unique, order, s, a, pair, rwidth, opts, name, infile
  31.     
  32.     unique := 0                        # switch to list unique usage only
  33.     order := 3                        # alphabetical ordering switch
  34.  
  35.     opts := getopt(args,"anu")[1]
  36.     if \opts["a"] then order := 3
  37.     if \opts["n"] then order := 4
  38.     if \opts["u"] then unique := 1
  39.  
  40.     while name := getfile("File?") do {
  41.         close(\infile)
  42.         infile := open(name) | {
  43.             write(&errout,"*** cannot open ",name)
  44.             next
  45.             }
  46.  
  47.         ccount := table(0)            # table of characters
  48.         while ccount[reads(infile)] +:= 1
  49.         a := sort(ccount,order)
  50.         if unique = 1 then {
  51.             while s := get(a) do
  52.                 if get(a) = 1 then write(s)
  53.              }
  54.         else {
  55.             rwidth := 0
  56.             every rwidth <:= *!a
  57.             while s := get(a) do
  58.                 write(left(image(s),10),right(get(a),rwidth))
  59.             }
  60.         }
  61. end
  62.